﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.Text;

namespace VSM
{
    public partial class Config : System.Web.UI.Page
    {
        private string TblHeaderName = "U_Config";
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("../../Login.aspx");
            }

            if (!IsPostBack)
            {
                this.hdnCustomerCode.Value = this.txtCustomerCode.Text = Session[Utils.AppConstants.ID_Dealer].ToString();
                if (Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM")
                {
                    this.txtC.Text = "0";
                    this.txtD.Text = "0";
                    this.txtD.Visible = false;
                    this.txtC.Visible = false;
                }
                else
                {
                    this.txtA.Text = "0";
                    this.txtB.Text = "0";
                    this.txtA.Visible = false;
                    this.txtB.Visible = false;
                }
                LoadData("VSM", this.hdnCustomerCode.Value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadData(string dbType, string CardCode)
        {
            MasterData obj = new MasterData();
            DataSet ds = obj.GetConfig(dbType, CardCode);
            if (ds != null && ds.Tables.Count > 0)
            {
                BindData(ds.Tables[0]);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tblData"></param>
        private void BindData(DataTable tblData)
        {
            if (tblData.Rows.Count > 0)
            {
                DataRow dr = tblData.Rows[0];
                this.hdnID.Value = dr["ID"].ToString();
                this.txtCustomerCode.Text = dr["CardCode"].ToString();
                this.txtOC.Text = dr["OC"].ToString();
                this.txtLT.Text = dr["LT"].ToString();
                this.txtA.Text = dr["MRPA"].ToString();
                this.txtB.Text = dr["MRPB"].ToString();
                this.txtC.Text = dr["MRPC"].ToString();
                this.txtD.Text = dr["MRPD"].ToString();
                this.txtSeries.Text = dr["Series"].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtOC.Text.Trim().Length == 0 ||
                    this.txtLT.Text.Trim().Length == 0 ||
                    this.txtSeries.Text.Trim().Length == 0 ||
                    this.txtA.Text.Trim().Length == 0 ||
                    this.txtB.Text.Trim().Length == 0 ||
                    this.txtC.Text.Trim().Length == 0 ||
                    this.txtD.Text.Trim().Length == 0)
                {
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('Missing infomation.','');", true);
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
                    return;
                }

                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                DataSet dsUpdate = CollectData();
                MasterData obj = new MasterData();
                string errMess = obj.UpdateConfig(dsUpdate, "VSM");
                if (errMess.Length > 0)
                {
                    Session["errorMessage"] = errMess;
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors",
                        "Main.setMasterMessage('" + errMess + "'", true);
                }
                else
                {
                    LoadData("VSM", Session[Utils.AppConstants.ID_Dealer].ToString());
                    Session["successMessage"] = "Operation complete sucessful!";
                    ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + "Operation complete sucessful!" + "','');", true);
                }
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + GeneralFunctions.UrlFullEncode(ex.ToString()) + "','');", true);
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataSet CollectData()
        {
            try
            {
                DataTable dt = new DataTable("U_Config");
                dt.Columns.Add("ID");
                dt.Columns.Add("CardCode");
                dt.Columns.Add("OC");
                dt.Columns.Add("LT");
                dt.Columns.Add("MRPA");
                dt.Columns.Add("MRPB");
                dt.Columns.Add("MRPC");
                dt.Columns.Add("MRPD");
                dt.Columns.Add("Series");

                DataRow dr = dt.NewRow();
                dr["ID"] = this.hdnID.Value;
                dr["CardCode"] = this.txtCustomerCode.Text;
                dr["OC"] = this.txtOC.Text;
                dr["LT"] = this.txtLT.Text;
                dr["MRPA"] = this.txtA.Text;
                dr["MRPB"] = this.txtB.Text;
                dr["MRPC"] = this.txtC.Text;
                dr["MRPD"] = this.txtD.Text;
                dr["Series"] = this.txtSeries.Text;
                dt.Rows.Add(dr);

                DocumentXML objInfo = new DocumentXML();
                DataSet ds = new DataSet("DS");
                dt.TableName = TblHeaderName;
                ds.Tables.Add(dt.Copy());
                return ds;
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "OKErrors", "Main.setMasterMessage('" + GeneralFunctions.UrlFullEncode(ex.ToString()) + "','');", true);
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "CloseLoading", "Dialog.hideLoader();", true);
                return null;
            }
            return null;
        }
    }
}